package com.hongdee.crm.carConfig.dao.impl;

import com.hongdee.base.basedao.impl.BaseDaoImpl;
import com.hongdee.crm.carConfig.dao.ITCrmCarRefDao;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by My on 2017/2/20.
 */
@Service
public class TCrmCarRefDaoImpl extends BaseDaoImpl implements ITCrmCarRefDao {

    /****
     *
     * @param company_id
     * @param uid
     * @return
     * @throws Exception
     */
    public List<Map<String, Object>> findCarRefByUid(String company_id, String uid) throws Exception {
        StringBuffer sqlStr = new StringBuffer();
        sqlStr.append("SELECT ");
        sqlStr.append(" * ");
        sqlStr.append("FROM ");
        sqlStr.append(" t_crm_car_ref ");
        sqlStr.append("WHERE ");
        sqlStr.append(" u_id = ?");
        sqlStr.append(" and company_id = ? and status=1");
        System.out.println(sqlStr.toString());
        List<Map<String, Object>> list = this.getJdbcDao().queryForMapListBySqlWithParams(sqlStr.toString(), uid, company_id);
        if (list.size() > 0) {
            return list;
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> findCompanyList() throws Exception {
        String sqlStr = "select * from t_crm_car_ref where status=1 group by company_id";
        return this.getJdbcDao().queryForMapListBySql(sqlStr);
    }

    /**
     * 获得处理权限的公司信息
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, Object>> findCompanyByTaskTypeList() throws Exception {
        String sqlStr = "select * from t_crm_task_conf where status='1' and task_type='1' group by company_id ";
        return this.getJdbcDao().queryForMapListBySql(sqlStr);
    }

    /**
     * 查询公司下的处理角色信息，以及角色对应的任务类型
     *
     * @param companyId
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, Object>> findRoleByCompanyIdList(String companyId) throws Exception {
        String sqlStr = "select role_id,GROUP_CONCAT(task_c_code) task_code from t_crm_task_conf where status='1' and task_type='1' and company_id=? GROUP BY role_id";
        return this.getJdbcDao().queryForMapListBySqlWithParams(sqlStr, companyId);
    }

    /**
     * 公司ID 查询设置的所有人员
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, Object>> findUserByCompnayIdList(String companyId) throws Exception {
        StringBuffer sqlStr = new StringBuffer();
        sqlStr.append("SELECT ");
        sqlStr.append(" * ");
        sqlStr.append("FROM ");
        sqlStr.append(" t_crm_car_ref ");
        sqlStr.append("WHERE ");
        sqlStr.append(" company_id = ? and status=1 ORDER BY type*1 DESC");
        System.out.println(sqlStr.toString());
        return this.getJdbcDao().queryForMapListBySqlWithParams(sqlStr.toString(), companyId);
    }

    /**
     * 根据公司ID、角色ID，获得角色下所对应的人员
     *
     * @param deptList
     * @param roleId
     * @return
     */
    @Override
    public List<Map<String, Object>> findRoleUserByCompanyIdAndRoleId(String roleId, List<String> deptList) throws Exception {
        StringBuffer sqlStr = new StringBuffer();
        List<String> paramLists = new ArrayList<>();
        sqlStr.append("select r.r_id,r.r_type,c.id as uid,c.u_name,c.o_id from t_bc_role_user r,t_bc_user c " +
                "where r.u_id=c.id  and r.r_id =? and r.r_type=0 and o_id in (");
        paramLists.add(roleId);
        for (int i = 0; i < deptList.size(); i++) {
            sqlStr.append("?");
            if (i < (deptList.size() - 1)) {
                sqlStr.append(",");
            }
        }
        sqlStr.append(")");
        paramLists.addAll(deptList);
        return this.getJdbcDao().queryForMapListBySqlWithParams(sqlStr.toString(), paramLists.toArray());
    }

}
